// pages/moremovie/moremovie.js
var util = require("../../../utils/util.js");
var app = getApp();
Page({
  data: {
    navtitle: '',
    movies: '',
    nexturl: '',
    startnum: 0,
    isempty: true,

  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var catetype = options.catetype;
    this.data.navtitle = catetype;
    var url = "";
    switch (catetype) {
      case "正在热映":
        url = app.globalData.g_doubanapi + "/v2/movie/in_theaters";
        break;
      case "即将上映":
        url = app.globalData.g_doubanapi + "/v2/movie/coming_soon";
        break;
      case "电影Top250":
        url = app.globalData.g_doubanapi + "/v2/movie/top250";
        break;
    }
    this.data.nexturl = url;
    util.httpGet(url, this.progressData);
  },
  onReachBottom: function (event) {
    var nexturl = this.data.nexturl + "?start=" + this.data.startnum + "&count=20";
    util.httpGet(nexturl, this.progressData);
    wx.showNavigationBarLoading();

  },
  onPullDownRefresh: function (event) {
    var refeurl = this.data.nexturl + "?start=0&count=20";
    this.data.isempty = true;
    util.httpGet(refeurl, this.progressData);
    wx.showNavigationBarLoading();

  },
  progressData: function (data) {
    var movie = [];
    var data = data.subjects;
    for (var i in data) {
      var title = data[i].title;
      if (title.length > 6) {
        title = title.substring(0, 6) + "...";
      }
      var stars = util.toArray(data[i].rating.stars);
      var tpldata = {
        title: title,
        average: data[i].rating.average,
        coverageurl: data[i].images.large,
        movieid: data[i].id,
        stars: stars

      };
      movie.push(tpldata);
    }
    var totaldata = {};
    if (!this.data.isempty) {
      totaldata = this.data.movies.concat(movie);
    } else {
      totaldata = movie;
      this.data.isempty = false;
    }
    this.setData({
      movies: totaldata
    });
    this.data.startnum += 20;
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();

  },
  onReady: function () {
    wx.setNavigationBarTitle({
      title: this.data.navtitle,
      success: function (res) {
        // success
        wx.hideNavigationBarLoading()
      }
    })

  }

})